package com.ruoyi.web.controller.questions.api;


import com.ruoyi.comment.service.IQuestionsService;
import com.ruoyi.common.annotation.Check;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;

import com.ruoyi.model.question.dto.QuestionDTO;
import com.ruoyi.model.question.vo.QuestionVO;
import com.ruoyi.sensitive.SensitiveDTO;
import com.ruoyi.sensitive.SensitiveServiceImplV1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : Afanda
 **/
@Api(tags = "前台：提问服务")
@Slf4j
@RestController
@RequestMapping("/api/questions")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class QuestionsController extends BaseController {

    /**
     * 敏感词服务
     */
    private final SensitiveServiceImplV1 sensitiveServiceImplV1;

    /**
     * 提问服务
     */
    private final IQuestionsService questionsService;

    @ApiOperation(value = "发布提问", notes = "使用敏感词工具类屏蔽")
    @PostMapping("/send-comment")
    @Check
    public AjaxResult sendComment(@ApiParam("评论的相关信息以及层级关系") @RequestBody QuestionDTO questionDTO) {
        if (StringUtils.isEmpty(questionDTO.getContent())) {
            throw new RuntimeException("发布评论信息不能为空!");
        } else {
            SensitiveDTO sensitiveDTO = sensitiveServiceImplV1.analysisText(questionDTO.getContent());
            String handleContent = sensitiveDTO.getHandleContent();
            questionDTO.setContent(handleContent);
            return toAjax(questionsService.sendCommentContent(questionDTO));
        }
    }

    @ApiOperation("根据id查询提问")
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable String id){

        QuestionVO questionVO = questionsService.queryById(id);
        return AjaxResult.success(questionVO);
    }

    @ApiOperation("根据id删除提问")
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable String id){

        questionsService.removeByIdQuestions(id);
        return AjaxResult.success();
    }

    @GetMapping("/{page}/{pageSize}")
    @ApiOperation("批量查询")
    public AjaxResult queryList(@PathVariable Integer page,@PathVariable Integer pageSize){
        List<QuestionVO> questionVOList = questionsService.queryList(page,pageSize);
        return AjaxResult.success(questionVOList);
    }

}
